package com.iretailer.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.iretailer.report.model.WeatherCode;
import com.iretailer.report.model.WindDirection;
import com.iretailer.report.service.LangDictService;
import com.iretailer.report.util.JsonUtil;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Random;

//@SpringBootTest
public class ImportLangData {

    @Autowired
    LangDictService langDictService;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    private Map<String, Object> jsonMap(String l) throws IOException {
        return JsonUtil.json2map(IOUtils.toString(new FileReader("C:\\Users\\Bennie.BENJAMIN\\Desktop\\" + l + ".json")));
    }

    @Test
    public void contextLoads() throws IOException {
//        Map<String, Object> cm = jsonMap("zh-cn");
//        Map<String, Object> em = jsonMap("en");
//        Map<String, Object> jm = jsonMap("ja");
//        HashSet<String> keySet = new HashSet<>();
//        keySet.addAll(cm.keySet());
//        keySet.addAll(em.keySet());
//        keySet.addAll(jm.keySet());
//        List<LangDict> langDictList = keySet.stream().map(key ->
//                new LangDict().setLangDictKey(key).setEnglishValue((String) em.get(key)).setChineseValue((String) cm.get(key)).setJapaneseValue((String) jm.get(key))
//        ).collect(Collectors.toList());
//        langDictService.saveBatch(langDictList);


        System.out.println("----------------------------------------------------------------------------------------------------------------");
        System.out.println(updateWeatherSql());
        System.out.println("----------------------------------------------------------------------------------------------------------------");
    }

    private String updateWeatherSql() {
        return Arrays.stream(WeatherCode.values()).map(weatherCode ->
                String.format("update t_weathers set WeatherName = '%s' where WeatherName='%s';\n",
                        weatherCode.getEn(),weatherCode.getZh())).reduce((pre, cur)
                -> pre += cur).orElse("");
    }

    private String insertWindDirectionSql() {
        return Arrays.stream(WindDirection.values()).map(windDirection -> {
                    String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis() - new Random().nextInt(Integer.MAX_VALUE) / 1000));
                    return String.format("INSERT INTO t_lang_dict (Id,LangDictKey,EnglishValue,JapaneseValue,ChineseValue,CreateTime) VALUES ('%s','%s','%s','%s','%s','%s');\n",
                            IdWorker.get32UUID(), windDirection.getEn(), windDirection.getEn(), windDirection.getEn(), windDirection.getZh(), dateTime);
                }
        ).reduce((p, c) -> p += c).orElse("");
    }

    private String insertWeatherSql() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cookie", "SRCHD=AF=BDVEHC; SRCHUID=V=2&GUID=AD2374CE127843BD83FD9B4B1DF2931A&dmnchg=1; _EDGE_V=1; MUID=031893B20186693F17059D6C00A868E3; MUIDB=031893B20186693F17059D6C00A868E3; _EDGE_S=mkt=zh-cn&ui=zh-cn&SID=07B009CA79396D2614AC072A78176C3A; ISSW=1; SRCHUSR=DOB=20200603&T=1591692956000; ipv6=hit=1591696558912&t=4; _SS=SID=07B009CA79396D2614AC072A78176C3A&bIm=686; _FP=hta=on; ENSEARCH=BENVER=0; SNRHOP=I=&TS=; MSCC=1; MSTC=ST=1; btstkn=0XzFfoPw8bMmm33xVaEJsROjVPwk7ImI%252BobaKgE%252BnY4K8vVoThrwoFjRcCw6c1TL; _tarLang=default=ja; _TTSS_OUT=hist=WyJlbiIsInpoLUhhbnMiLCJqYSJd; SRCHHPGUSR=HV=1591693126&WTS=63727289756&CW=2048&CH=1009&DPR=1&UTC=480; _TTSS_IN=hist=WyJqYSIsInpoLUhhbnMiLCJhdXRvLWRldGVjdCJd");
        headers.add("content-type", "application/x-www-form-urlencoded");
        return Arrays.stream(WeatherCode.values()).map(weatherCode -> {
                    MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
                    postParameters.add("fromLang", "zh-Hans");
                    postParameters.add("to", "ja");
                    postParameters.add("text", weatherCode.getZh());
                    HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(postParameters, headers);
                    String result = restTemplate.postForObject("https://cn.bing.com/ttranslatev3?isVertical=1&&IG=E1C9E1F53E834C179EAA0638FD39C3A8&IID=translator.5028.4", r, String.class);
                    JSONArray jsonArray = JSON.parseArray(result);
                    assert jsonArray != null;
                    String ja = jsonArray.getJSONObject(0).getJSONArray("translations").getJSONObject(0).getString("text");
                    String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis() - new Random().nextInt(Integer.MAX_VALUE) / 1000));
                    return String.format("INSERT INTO t_lang_dict (Id,LangDictKey,EnglishValue,JapaneseValue,ChineseValue,CreateTime) VALUES ('%s','%s','%s','%s','%s','%s');\n",
                            IdWorker.get32UUID(), weatherCode.getEn(), weatherCode.getEn(), ja, weatherCode.getZh(), dateTime);
                }
        ).reduce((p, c) -> p += c).orElse("");

    }


}